package com.hippo.core.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @ClassName MyMetaObjectHandler
 * @Description TODO 配置mybatis数据填充规则
 * @Author tangxl
 * @create 2023-04-25 11:15
 **/
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("插入时自动填充策略");
        // 获取到需要被填充的字段值,填充数据
        // 创建时间
        Object createTime = getFieldValByName("create_time", metaObject);
        if (createTime == null) {
            setFieldValByName("create_time", LocalDateTime.now(ZoneId.of("Asia/Shanghai")), metaObject);
        }
        // 更新时间
        Object updateTime = getFieldValByName("update_time", metaObject);
        if (updateTime == null) {
            setFieldValByName("update_time", LocalDateTime.now(ZoneId.of("Asia/Shanghai")), metaObject);
        }
        // 数据状态
        Object status = getFieldValByName("status", metaObject);
        if (status == null) {
            setFieldValByName("status", 1, metaObject);
        }
//        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("更新时自动填充策略");
        // 获取到需要被填充的字段值,填充数据
        // 更新时间
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateTime == null) {
            setFieldValByName("updateTime", LocalDateTime.now(ZoneId.of("Asia/Shanghai")), metaObject);
        }

//        this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
    }
}
